package com.sskj.wallet.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.AddressAllBean;
import com.sskj.wallet.presenter.AddressListPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.WALLET_ACTIVITY_ADDRESS_LIST)
public class AddressListActivity extends BaseActivity<AddressListPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.iv_add_btc)
    ImageView ivAddBtc;
    @BindView(R2.id.recyclerViewBtc)
    RecyclerView recyclerViewBtc;
    @BindView(R2.id.iv_add_eth)
    ImageView ivAddEth;
    @BindView(R2.id.recyclerViewEth)
    RecyclerView recyclerViewEth;
    @BindView(R2.id.iv_add_usdt)
    ImageView ivAddUsdt;
    @BindView(R2.id.recyclerViewUsdt)
    RecyclerView recyclerViewUsdt;
    @BindView(R2.id.iv_add_aicash)
    ImageView ivAddAicash;
    @BindView(R2.id.recyclerViewAicash)
    RecyclerView recyclerViewAicash;
    @BindView(R2.id.iv_add_ltc)
    ImageView ivAddLtc;
    @BindView(R2.id.recyclerViewLtc)
    RecyclerView recyclerViewLtc;

    private SlimAdapter slimAdapterBtc;
    private SlimAdapter slimAdapterEth;
    private SlimAdapter slimAdapterUsdt;
    private SlimAdapter slimAdapterAicash;
    private SlimAdapter slimAdapterLtc;

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_address_list;
    }

    @Override
    public AddressListPresenter getPresenter() {
        return new AddressListPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_addressListActivity1));
        initRecyclerView();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getAddressList();
    }

    private void initRecyclerView() {
        recyclerViewBtc.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterBtc = SlimAdapter.create().register(R.layout.wallet_item_address, new SlimInjector<AddressAllBean.BtcBean>() {
            @Override
            public void onInject(AddressAllBean.BtcBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_address_name, data.getNotes())
                        .text(R.id.tv_address_note, data.getQiaobao_url())
                        .clicked(R.id.tv_address_delete, v -> {
                            mPresenter.deleteAddress(data, Constans.ADDRESS_BTC);
                        })
                        .clicked(R.id.constraint, v -> {
                            Intent intent = new Intent();
                            intent.putExtra("addressId", data.getQiaobao_url());
                            setResult(RESULT_OK, intent);
                            finish();
                        });
            }
        }).attachTo(recyclerViewBtc).updateData(new ArrayList());

        recyclerViewEth.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEth = SlimAdapter.create().register(R.layout.wallet_item_address, new SlimInjector<AddressAllBean.BtcBean>() {
            @Override
            public void onInject(AddressAllBean.BtcBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_address_name, data.getNotes())
                        .text(R.id.tv_address_note, data.getQiaobao_url())
                        .clicked(R.id.tv_address_delete, v -> {
                            mPresenter.deleteAddress(data, Constans.ADDRESS_ETH);
                        })
                        .clicked(R.id.constraint, v -> {
                            Intent intent = new Intent();
                            intent.putExtra("addressId", data.getQiaobao_url());
                            setResult(RESULT_OK, intent);
                            finish();
                        });
            }
        }).attachTo(recyclerViewEth).updateData(new ArrayList());

        recyclerViewUsdt.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterUsdt = SlimAdapter.create().register(R.layout.wallet_item_address, new SlimInjector<AddressAllBean.BtcBean>() {
            @Override
            public void onInject(AddressAllBean.BtcBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_address_name, data.getNotes())
                        .text(R.id.tv_address_note, data.getQiaobao_url())
                        .clicked(R.id.tv_address_delete, v -> {
                            mPresenter.deleteAddress(data, Constans.ADDRESS_USDT);
                        })
                        .clicked(R.id.constraint, v -> {
                            Intent intent = new Intent();
                            intent.putExtra("addressId", data.getQiaobao_url());
                            setResult(RESULT_OK, intent);
                            finish();
                        });
            }
        }).attachTo(recyclerViewUsdt).updateData(new ArrayList());
        recyclerViewAicash.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterAicash = SlimAdapter.create().register(R.layout.wallet_item_address, new SlimInjector<AddressAllBean.BtcBean>() {
            @Override
            public void onInject(AddressAllBean.BtcBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_address_name, data.getNotes())
                        .text(R.id.tv_address_note, data.getQiaobao_url())
                        .clicked(R.id.tv_address_delete, v -> {
                            mPresenter.deleteAddress(data, Constans.ADDRESS_AICASH);
                        })
                        .clicked(R.id.constraint, v -> {
                            Intent intent = new Intent();
                            intent.putExtra("addressId", data.getQiaobao_url());
                            setResult(RESULT_OK, intent);
                            finish();
                        });
            }
        }).attachTo(recyclerViewUsdt).updateData(new ArrayList());
        recyclerViewLtc.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterLtc = SlimAdapter.create().register(R.layout.wallet_item_address, new SlimInjector<AddressAllBean.BtcBean>() {
            @Override
            public void onInject(AddressAllBean.BtcBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_address_name, data.getNotes())
                        .text(R.id.tv_address_note, data.getQiaobao_url())
                        .clicked(R.id.tv_address_delete, v -> {
                            mPresenter.deleteAddress(data, Constans.ADDRESS_LTC);
                        })
                        .clicked(R.id.constraint, v -> {
                            Intent intent = new Intent();
                            intent.putExtra("addressId", data.getQiaobao_url());
                            setResult(RESULT_OK, intent);
                            finish();
                        });
            }
        }).attachTo(recyclerViewUsdt).updateData(new ArrayList());
    }

    @Override
    public void initData() {
        mPresenter.getAddressList();
        //添加 btc 地址
        ClickUtil.click(300, ivAddBtc, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_ADD_ADDRESS).withString(Constans.ADDRESS_TYPE, Constans.ADDRESS_BTC).navigation();
        });
        //添加 eth 地址
        ClickUtil.click(300, ivAddEth, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_ADD_ADDRESS).withString(Constans.ADDRESS_TYPE, Constans.ADDRESS_ETH).navigation();
        });
        //添加 usdt 地址
        ClickUtil.click(300, ivAddUsdt, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_ADD_ADDRESS).withString(Constans.ADDRESS_TYPE, Constans.ADDRESS_USDT).navigation();
        });
        //添加 ltc 地址
        ClickUtil.click(300, ivAddLtc, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_ADD_ADDRESS).withString(Constans.ADDRESS_TYPE, Constans.ADDRESS_LTC).navigation();
        });
        //添加 aicash 地址
        ClickUtil.click(300, ivAddAicash, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_ADD_ADDRESS).withString(Constans.ADDRESS_TYPE, Constans.ADDRESS_AICASH).navigation();
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddressListActivity.class);
        context.startActivity(intent);
    }

    public void getAddressListOK(AddressAllBean data) {
        if (data == null) {
            return;
        }
        slimAdapterBtc.updateData(data.getBtc());
        slimAdapterEth.updateData(data.getEth());
        slimAdapterUsdt.updateData(data.getUsdt());
        slimAdapterAicash.updateData(data.getAicash());
        slimAdapterLtc.updateData(data.getLtc());
    }

}
